package com.itheima.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检查用户是否登录
 */
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    // 路径匹配器,支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 1:获取本次请求的URI
        String requestURI = request.getRequestURI();

        log.info("拦截到请求: {}",requestURI);
        // 定义不需要处理的路径
        String[] urls = new String[]{
          "/employee/login",
          "/employee/logout",
          "/backend/**",
          "/front/**",
          "/common/**",
          "/user/sendSms",  // 移动端发送短信
          "/user/login"     // 移动端登录
        };
        // 2:判断本次请求, 是否需要登录, 才可以访问,定义一个方法用来检查
        boolean check = check(urls, requestURI);
        // 3:如果不需要，则直接放行
        if (check){
            log.info("本次请求不需要处理: {}",requestURI);

            filterChain.doFilter(request,response);
            return;
        }
        // 4-1:判断登录状态，如果已登录，则直接放行
        if (request.getSession().getAttribute("employee") != null){
            log.info("用户已登录,用户的id为:{}",request.getSession().getAttribute("employee"));

            // 往线程中设置id
            Long empId = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

            filterChain.doFilter(request,response);

            BaseContext.removeCurrentId();
            return;
        }

        // 4-2:判断登录状态，如果已登录，则直接放行
        if (request.getSession().getAttribute("user") != null){
            log.info("用户已登录,用户的id为:{}",request.getSession().getAttribute("user"));

            // 往线程中设置id
            Long userId = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(userId);

            filterChain.doFilter(request,response);

            BaseContext.removeCurrentId();
            return;
        }

        log.info("用户未登录");
        // 5:如果未登录, 则返回未登录结果,通过输出流方式向客户端页面响应数据
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;
    }

    public static boolean isAjaxRequest(HttpServletRequest request) {

        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1){
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1){
            return true;
        }

        String uri = request.getRequestURI();
        if (StringUtils.endsWithAny(uri, new String[]{".json", ".xml"})) {
            return true;
        }

        String ajax = request.getParameter("__ajax");
        if (StringUtils.endsWithAny(ajax, new String[]{"json", "xml"})){
            return true;
        }

        return false;

    }

    /**
     * 路径匹配,检查本次请求是否需要进行处理
     * @param urls
     * @param requestURI
     * @return
     */
    public boolean check(String[] urls,String requestURI){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match){
                return true;
            }
        }
        return false;
    }
}
